package info.touret.winecellar.producer.server.domain;

import info.touret.winecellar.dao.server.DataStoreObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.persistence.Basic;
import javax.validation.constraints.NotNull;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.googlecode.objectify.annotation.Entity;

@Entity
public class Producer extends DataStoreObject<Producer> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Producer() {
	}

	@Basic
	@NotNull
	private String name;
	@Basic
	private String address;
	@Basic
	private String zip;
	@Basic
	private String city;
	@Basic
	private String country;
	@Basic
	private String phone;
	@Basic
	@NotNull
	private String domain;
	@Basic
	@NotNull
	private String region;

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the zip
	 */
	public String getZip() {
		return zip;
	}

	/**
	 * @param zip
	 *            the zip to set
	 */
	public void setZip(String zip) {
		this.zip = zip;
	}

	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @param city
	 *            the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/**
	 * @return the country
	 */
	public String getCountry() {
		return country;
	}

	/**
	 * @param country
	 *            the country to set
	 */
	public void setCountry(String country) {
		this.country = country;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone
	 *            the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;

	}

	public Producer(String name, String address, String zip, String city,
			String country, String phone, String domain) {
		this.name = name;
		this.address = address;
		this.zip = zip;
		this.city = city;
		this.country = country;
		this.phone = phone;
		this.domain = domain;
	}

	@Override
	public String toString() {
		return "NOM:" + getName() + "|ADDR:" + getAddress() + "|VILLE:"
				+ getCity();
	}

	private Long user;

	public Long getUser() {
		return user;
	}

	public void setUser(Long user) {
		this.user = user;
	}

	@SuppressWarnings("unchecked")
	public static List<Producer> findAllProducers() {
		Objectify objectify = ObjectifyService.begin();
		List<Producer> list = objectify.query(Producer.class)
				.filter("userID", retreiveCurrentUserID()).list();
		list.size();
		return list;
	}

	public static Producer findProducer(Long id) {
		Objectify objectify = ObjectifyService.begin();
		return objectify.find(Producer.class, id);
	}

	@SuppressWarnings("unchecked")
	public static List<Producer> findProducersByCriteria(String p) {
		List<Producer> producers = new ArrayList<Producer>();
		Objectify objectify = ObjectifyService.begin();
		Query<Producer> query = objectify.query(Producer.class);
		List<Producer> allproducers = query.list();
		Pattern pattern = Pattern.compile(".*" + p + ".*");

		for (Producer current : allproducers) {
			if (pattern.matcher(current.getName()).find()
					|| pattern.matcher(current.getRegion()).find()
					|| pattern.matcher(current.getDomain()).find()
					|| pattern.matcher(current.getCity()).find()) {
				producers.add(current);
			}
		}
		return producers;
	}
}
